<?php
/* This file is part of Mirasol CMS
   (C) 2011-2012 by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

if (!defined ("_XMLFEED_PHP_"))
{
  define ("_XMLFEED_PHP_", "1");

  function xmlfeed_fields ($xmlfeed)
  {
    $fields = array ();
    
    /* Prepare the XML parser and parse the data */
    $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$xmlfeed;
    $xmlres = xml_parser_create ("utf-8");
    xml_parse_into_struct ($xmlres, $xmldata, $values);
    xml_parser_free ($xmlres);
 
    /* Walk through the tags */
    $i = 0;
    $n = 0;
    while (!empty ($values[$i]))
      {
        /* Find the field tag */
        if ($values[$i]['level'] == 2 && strcasecmp ($values[$i]['tag'], "field") == 0)
          {
            $fields[$n] = $values[$i]['attributes']['NAME'];
            $n++;
          }
        $i++;
      }
    sort ($fields);
    return $fields;
  }
  
  function xmlfeed_numhtmlfields ($fields)
  {
    $num = 0;
    
    /* Prepare the XML parser and parse the data */
    $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$fields;
    $xmlres = xml_parser_create ("utf-8");
    xml_parse_into_struct ($xmlres, $xmldata, $values);
    xml_parser_free ($xmlres);
    
    /* Walk through the tags until we find the html fields */
    $i = 0;
    while (!empty ($values[$i]))
      {
        /* Find the html type */
        if ($values[$i]['level'] == 2 && strcasecmp ($values[$i]['tag'], "field") == 0 && strcasecmp ($values[$i]['attributes']['TYPE'], "html") == 0)
          $num++;
        $i++;
      }
    return $num;
  }
  
  function xmlfeed_title ($xmlfeed)
  {
    /* Prepare the XML parser and parse the data */
    $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$xmlfeed;
    $xmlres = xml_parser_create ("utf-8");
    xml_parse_into_struct ($xmlres, $xmldata, $values);
    xml_parser_free ($xmlres);
    
    /* Walk through the tags until we find the title tag */
    $i = 0;
    while (!empty ($values[$i]))
      {
        /* Find the title tag */
        if ($values[$i]['level'] == 2 && strcasecmp ($values[$i]['tag'], "title") == 0)
          {
            $ret = $values[$i]['value'];
            break;
          }
        $i++;
      }
    return isset ($ret) ? $ret : "";
  }
  
  function xmlfeed_value ($xmlfeed, $name)
  {
    /* Prepare the XML parser and parse the data */
    $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$xmlfeed;
    $xmlres = xml_parser_create ("utf-8");
    xml_parse_into_struct ($xmlres, $xmldata, $values);
    xml_parser_free ($xmlres);

    /* Walk through the tags until we find the right field */
    $i = 0;
    while (!empty ($values[$i]))
      {
        /* Find the title tag */
        if ($values[$i]['level'] == 3 && strcasecmp ($values[$i]['tag'], $name) == 0)
          {
            $ret = $values[$i]['value'];
            break;
          }
        $i++;
      }
    return isset ($ret) ? $ret : "";
  }
}
?>
